package com.cyy.dyeing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.dyeing.common.ProcessDefEnum;
import com.cyy.dyeing.dto.ProcessNode;
import com.cyy.dyeing.exception.BusinessException;
import com.cyy.dyeing.mapper.ProcessNodeMapper;
import com.cyy.dyeing.service.ProcessNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: cyy
 * @Date: 2024/7/28
 */
@Service
public class ProcessNodeServiceImpl extends ServiceImpl<ProcessNodeMapper, ProcessNode> implements ProcessNodeService {

    @Autowired
   private ProcessNodeMapper mapper;

    /**
     * 更新状态为非当前状态
     * @param groupId
     * @param processDefCls
     */
    @Override
    public void updateAllGroupProcessNodeCurrFalse(String groupId, int processDefCls) {
        mapper.updateAllGroupProcessNodeCurrFalse(groupId, processDefCls);

    }

    @Override
    public void updateProcessNode(ProcessNode pn) {
        LambdaUpdateWrapper<ProcessNode> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(pn.getGroup_id()!=null, ProcessNode::getGroup_id, pn.getGroup_id());
        wrapper.eq(pn.getProduct_id()!=null, ProcessNode::getProduct_id, pn.getProduct_id());
        wrapper.eq(pn.getStore_id()!=null, ProcessNode::getStore_id, pn.getStore_id());
        wrapper.eq(ProcessNode::getPd_id, pn.getPd_id());
        update(pn, wrapper);
    }

    @Override
    public void  updateProcessNode(ProcessNode pn, int processDefCls){
        if(pn.getPn_curr()){
            if (processDefCls == 1){
                updateAllGroupProcessNodeCurrFalse(pn.getGroup_id(), processDefCls);
            } else if (processDefCls == 2) {
                //TODO
            } else if (processDefCls == 3) {
                //TODO
            }
        }
        updateProcessNode(pn);
    }

    @Override
    public ProcessNode findProcessNodeByGroupIdAndPdId(ProcessNode processNode) {
        LambdaQueryWrapper<ProcessNode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessNode::getGroup_id, processNode.getGroup_id());
        wrapper.eq(ProcessNode::getPd_id, processNode.getPd_id());
        return getOne(wrapper);
    }

}
